// pages/settlement/AddGoods/AddGoods.js
const controllers = require('../../controllers/controllerIndex.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    labels:[],
    rightList:[],
    
    labelId:0,
    navchoice:0,
    imgurl:'test',
    pageNumber: 1,//页数
    pageSize:5,//页大小
    chooseList:[],
    chooseProduct:[],
    onlyone:false,
  }, 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this
    console.log(options)
    if( options.nowList && options.nowList!='undefined'&& options.nowList.length>0){
      let  chooseList =JSON.parse( options.nowList)
      that.setData({
        chooseList: chooseList,
        chooseProduct:chooseList
      });
    }
    if(options.onlyone){
      this.setData({
        onlyone:options.onlyone
      })
    }
  
    controllers.selectLabel({
      type: 1,//0默认1商城页面分类列表2店铺入驻分类列表3系统新增商城分类
      shopId:wx.getStorageSync('shopId'),
      
    }).then(res =>{
      if(res.data.length>0){
     that.setData({
      labels:res.data,
      navchoice:res.data[0].id
      })
      that.getList(1);}
    })
    
  },
  lower(e){
   console.log(e)
    let that =this
    if(!that.data.lastPage){
      let p=that.data.pageNumber
     
      that.getList(p+1)
    }
  },
  //右侧内容
  getList(page){
    var that =this;
    var page=page||1
   
    var jsonInfo ={
      shopId:wx.getStorageSync('shopId'),
      labelId: that.data.navchoice,
      pageSize:20,
      pageNumber:page,
    }



    controllers.labelGoodsPage(jsonInfo).then(res =>{
     
      let   rightList=that.data.rightList
       
        if(res.data.pageNumber!=1){
         
          console.log(1,res.data.list)
          rightList=rightList.concat(res.data.list)
          console.log(2,rightList)
        }
        else{
          rightList=res.data.list
          console.log(3,rightList)
        
        }

      // for (var index in rightList){
      //   rightList[index].count =1;
      // }
      
      that.setData({
        rightList:rightList,
        lastPage: res.data.lastPage,
        pageNumber:res.data.pageNumber
      })

      
      rightList.forEach(function (v, index) {
        that.setData({
          [`rightList[${index}].count`]:1
        })

        
        that.data.chooseList.forEach(item => {
          if (item.id == v.id) {
            that.setData({
              [`rightList[${index}].checked`]: true,
              [`rightList[${index}].count`]:item.count,
             
            });
          }
        })
 
      
     })


    })
  },
  
  add(res){
    console.log(res)
      let that =this
    that.data.rightList.forEach(function (v, index) {
      if (res.target.dataset.id == v.id) {
        if(v.type==0){
          that.setData({
            [`rightList[${index}].count`]:1,
            [`rightList[${index}].checked`]:true
          })
        }else if(v.type == 1){
          that.setData({
            [`rightList[${index}].count`]:res.detail,
            [`rightList[${index}].checked`]:true
          })
        }else{
          wx.showToast({
            title: '无法添加云商品',
            icon: 'none',
            duration: 2000
          })
           
        }
    
    }
    })
    this.addChooseList()
  },

  //点击左边导航
  navchoice(e){
    this.setData({
      navchoice:e.currentTarget.id,
      pageNumber: 1,  scrollTop:0
    })
    this.getList(1);
  },

  //选中
  checkboxChange(e) {
    console.log(e);
    let that = this;
    const values = e.detail.value;
    var items= that.data.rightList;
    var preReplace = [];
    if(that.data.onlyone){
      preReplace=[]
    }else{
      preReplace=that.data.chooseProduct;
    }
    for (let i = 0, lenI = that.data.rightList.length; i < lenI; ++i) {
      items[i].checked = false
      items[i].count=1
      let chooseIndex = that.findIndex(items[i].id)
      console.log(chooseIndex)
      if(chooseIndex!=-1){
        preReplace.splice(chooseIndex, 1); 
      }
      
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if(that.data.onlyone){
          items[i].checked=false
        }
        if (items[i].id == values[j]) {
          if(items[i].type==2){
            wx.showToast({
              title: '无法添加云商品',
              icon: 'none',
              duration: 2000
            })
          }else{
            items[i].checked = true
            items[i].count =values[j].count||1
          }
        }
      }
    }
    
  
    this.setData({
      rightList:items,
      chooseProduct:preReplace,
      chooseList:preReplace
    })

    this.addChooseList()
  },
  findIndex(value){
   return this.data.chooseProduct.findIndex(item => {
      return item.id == value;
  });

  },
  addChooseList(){
    let that =this;
      let chooseProduct =this.data.chooseProduct;
      this.data.rightList.forEach(element => {
          if(element.checked==true){
            if(that.findIndex(element.id)==-1)
            chooseProduct.push(element)
          }
        });
          this.setData({
            chooseProduct
          })
    },

  cancel(){
    wx.navigateBack({
  
    })

  },
  returnBychoose(){
   
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];  //上一个页面
    let chooseProduct =this.data.chooseProduct;
    prevPage.setData({
      chooseProduct: chooseProduct
    })
    wx.navigateBack({
  
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})